Ext.namespace("BranchTree");
BranchTree.list = function(pp,ids){
	this.parentPanel = pp;
	this.ids={ids:this.ids};
	//TreePanel配置参数
//	1.animate:true//展开,收缩动画,false时,则没有动画效果
//	2.autoHeight:true//自动高度,默认为false
//	3.enableDrag:true//树的节点可以拖动Drag(效果上是),注意不是Draggable
//	4.enableDD:true//不仅可以拖动,还可以通过Drag改变节点的层次结构(drap和drop)
//	5.enableDrop:true//仅仅drop
//	6.lines:true//节点间的虚线条
//	7.loader:Ext.tree.TreeLoader//加载节点数据
//	8.root:Ext.tree.TreeNode//根节点
//	9.rootVisible:false//false不显示根节点,默认为true
//	10.trackMouseOver:false//false则mouseover无效果
//	11.useArrows:true//小箭头
	this.treepanel = new Ext.tree.TreePanel({
    	autoScroll : true,
    	collapsible:true,
    	useArrows:true,
    	animate:true,
    	width:250,
    	region:'center',
    	frame:true,
    	split:true,
    	onlyLeafCheckable: true,
    	loader : new Ext.tree.TreeLoader({
            dataUrl : '../branch/getBranch.form'
        }),
        root:{
        	id: '0',
			text: '东区分公司',
			nodeType: 'async',
			//iconCls: 'icon-group',
			draggable: false,
			expanded:true
			//checked:false
        },
   	   listeners:{
			click:function(node, event){
				if (node.leaf == false) {//不是子节点
					if (node.expanded == false) {
						node.expand();// 展开
					} else {
						node.collapse();// 收起
					}
				 }
			},
			checkchange:function(node, checked){
				node.expand();// 展开
				var parentNode = node.parentNode;
	            node.attributes.checked = checked;     //父节点选中,选中所有子节点
	            node.eachChild(function(child) {     
		            child.ui.toggleCheck(checked);     
		            child.attributes.checked = checked;     
		            child.fireEvent('checkchange', child, checked);
	            });     
			},			
			beforeload:function(node){//2.单击节点前加载该节点的子节点
				if(node.id!=0){
					this.loader.dataUrl = '../branch/getRoles.form?branch_id='+node.id;
				}else{
					this.loader.dataUrl ='../branch/getBranch.form';
				}
			},
			render:function(tree){
				tree.expand();// 1.展开
			}
		}
	});
	this.saveButton = new Ext.Button({
		text:"确定",
		handler:function(){this.saveOrUpdateOrder();},
		scope:this
	});
	
	this.colseButton = new Ext.Button({
		text:"关闭",
		handler:function(){
			this.destroy();
		},
		scope:this
	});
	var bodyHeight = document.documentElement.clientHeight-30;
	BranchTree.list.superclass.constructor.call(this,{
		title:'部门角色树',
		width:300,
		height:350,
		modal:true,
		autoScroll:true,
		closeAction:'hide',
		layout:'border',
		buttonAlign:'right',
		buttons:[this.saveButton,this.colseButton],
		items:[this.treepanel]
	});
}
Ext.extend(BranchTree.list,Ext.Window,{
	init: function(ids) {
		for(var i=0;i<ids.length;i++){
			
		}
	},
	saveOrUpdateOrder:function(){
		var nodeArray = this.treepanel.getChecked();
		var nodeIds=[];
		var nodeTexts=[];
		for(var i =0;i<nodeArray.length;i++){
			var parentNodeText= nodeArray[i].parentNode.text
			nodeTexts.push(parentNodeText+"-"+nodeArray[i].text);
			nodeIds.push(nodeArray[i].id);
		}
		this.parentPanel.setIds1(nodeIds,nodeTexts);
		this.hide();                
	}
});
